Security News
GitHub Removes Malicious Pull Requests Targeting Open Source Repositories
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
simple-get
Advanced tools
Simplest way to make http get requests. Supports HTTPS, redirects, gzip/deflate, streams in < 100 lines.
The simple-get npm package is designed to provide a straightforward way to make HTTP requests in Node.js. It is built to be minimalistic but flexible, allowing users to perform GET, POST, and other HTTP methods with ease. It supports features like redirects, streaming, and simple error handling.
GET Request
This code sample demonstrates how to perform a simple GET request to a specified URL and stream the response to stdout. It also shows basic error handling.
const get = require('simple-get')
get('http://example.com', function (err, res) {
if (err) throw err
console.log(res.statusCode) // 200
res.pipe(process.stdout) // Stream the response to stdout
})
POST Request
This example illustrates how to send a POST request with a body of data. It demonstrates setting options for the request, including the URL, method, and body.
const get = require('simple-get')
const opts = {
url: 'http://example.com',
body: 'some data',
method: 'POST'
}
get(opts, function (err, res) {
if (err) throw err
console.log(res.statusCode) // 200
res.pipe(process.stdout) // Stream the response to stdout
})
Handling Redirects
This snippet shows how to handle redirects automatically by setting the 'followRedirects' option to true. It makes a request and follows any redirects, eventually logging the response status code of the final URL.
const get = require('simple-get')
get({ url: 'http://example.com', followRedirects: true }, function (err, res) {
if (err) throw err
console.log('Response from final URL:', res.statusCode)
res.pipe(process.stdout)
})
Axios is a promise-based HTTP client for the browser and Node.js. It provides a more feature-rich API compared to simple-get, including support for interceptors, automatic transforms for JSON data, and client-side protection against XSRF. It is more suitable for complex applications that require detailed configuration of HTTP requests.
node-fetch is a light-weight module that brings the Fetch API to Node.js. It offers a similar simplicity to simple-get but is designed to closely mimic the browser's fetch API, making it ideal for developers familiar with front-end development. Unlike simple-get, node-fetch returns promises and supports the Fetch API's full feature set.
Request is a comprehensive HTTP client for Node.js that supports HTTPS, redirects, streams, and more. Although it has been deprecated, it was known for its rich feature set and flexibility. Compared to simple-get, request offered a more extensive set of options for configuring requests, making it suitable for complex scenarios.
This module is the lightest possible wrapper on top of node.js http
, but supporting these essential features:
url
key so there's no need to use url.parse
on the url when specifying optionsAll this in < 120 lines of code.
npm install simple-get
Note, all these examples also work in the browser with browserify.
Doesn't get easier than this:
const get = require('simple-get')
get('http://example.com', function (err, res) {
if (err) throw err
console.log(res.statusCode) // 200
res.pipe(process.stdout) // `res` is a stream
})
If you just want the data, and don't want to deal with streams:
const get = require('simple-get')
get.concat('http://example.com', function (err, res, data) {
if (err) throw err
console.log(res.statusCode) // 200
console.log(data) // Buffer('this is the server response')
})
For POST
, call get.post
or use option { method: 'POST' }
.
const get = require('simple-get')
const opts = {
url: 'http://example.com',
body: 'this is the POST body'
}
get.post(opts, function (err, res) {
if (err) throw err
res.pipe(process.stdout) // `res` is a stream
})
const get = require('simple-get')
get({
url: 'http://example.com',
method: 'POST',
body: 'this is the POST body',
// simple-get accepts all options that node.js `http` accepts
// See: http://nodejs.org/api/http.html#http_http_request_options_callback
headers: {
'user-agent': 'my cool app'
}
}, function (err, res) {
if (err) throw err
// All properties/methods from http.IncomingResponse are available,
// even if a gunzip/inflate transform stream was returned.
// See: http://nodejs.org/api/http.html#http_http_incomingmessage
res.setTimeout(10000)
console.log(res.headers)
res.on('data', function (chunk) {
// `chunk` is the decoded response, after it's been gunzipped or inflated
// (if applicable)
console.log('got a chunk of the response: ' + chunk)
}))
})
You can serialize/deserialize request and response with JSON:
const get = require('simple-get')
const opts = {
method: 'POST',
url: 'http://example.com',
body: {
key: 'value'
},
json: true
}
get.concat(opts, function (err, res, data) {
if (err) throw err
console.log(data.key) // `data` is an object
})
You can set a timeout (in milliseconds) on the request with the timeout
option.
If the request takes longer than timeout
to complete, then the entire request
will fail with an Error
.
const get = require('simple-get')
const opts = {
url: 'http://example.com',
timeout: 2000 // 2 second timeout
}
get(opts, function (err, res) {})
It's a good idea to set the 'user-agent'
header so the provider can more easily
see how their resource is used.
const get = require('simple-get')
const pkg = require('./package.json')
get('http://example.com', {
headers: {
'user-agent': `my-module/${pkg.version} (https://github.com/username/my-module)`
}
})
You can use the tunnel
module with the
agent
option to work with proxies:
const get = require('simple-get')
const tunnel = require('tunnel')
const opts = {
url: 'http://example.com',
agent: tunnel.httpOverHttp({
proxy: {
host: 'localhost'
}
})
}
get(opts, function (err, res) {})
You can use the cookie
module to include
cookies in a request:
const get = require('simple-get')
const cookie = require('cookie')
const opts = {
url: 'http://example.com',
headers: {
cookie: cookie.serialize('foo', 'bar')
}
}
get(opts, function (err, res) {})
You can use the form-data
module to
create POST request with form data:
const fs = require('fs')
const get = require('simple-get')
const FormData = require('form-data')
const form = new FormData()
form.append('my_file', fs.createReadStream('/foo/bar.jpg'))
const opts = {
url: 'http://example.com',
body: form
}
get.post(opts, function (err, res) {})
application/x-www-form-urlencoded
form data manually:const get = require('simple-get')
const opts = {
url: 'http://example.com',
form: {
key: 'value'
}
}
get.post(opts, function (err, res) {})
You can use the oauth-1.0a
module to create
a signed OAuth request:
const get = require('simple-get')
const crypto = require('crypto')
const OAuth = require('oauth-1.0a')
const oauth = OAuth({
consumer: {
key: process.env.CONSUMER_KEY,
secret: process.env.CONSUMER_SECRET
},
signature_method: 'HMAC-SHA1',
hash_function: (baseString, key) => crypto.createHmac('sha1', key).update(baseString).digest('base64')
})
const token = {
key: process.env.ACCESS_TOKEN,
secret: process.env.ACCESS_TOKEN_SECRET
}
const url = 'https://api.twitter.com/1.1/statuses/home_timeline.json'
const opts = {
url: url,
headers: oauth.toHeader(oauth.authorize({url, method: 'GET'}, token)),
json: true
}
get(opts, function (err, res) {})
You can use limiter to throttle requests. This is useful when calling an API that is rate limited.
const simpleGet = require('simple-get')
const RateLimiter = require('limiter').RateLimiter
const limiter = new RateLimiter(1, 'second')
const get = (opts, cb) => limiter.removeTokens(1, () => simpleGet(opts, cb))
get.concat = (opts, cb) => limiter.removeTokens(1, () => simpleGet.concat(opts, cb))
var opts = {
url: 'http://example.com'
}
get.concat(opts, processResult)
get.concat(opts, processResult)
function processResult (err, res, data) {
if (err) throw err
console.log(data.toString())
}
MIT. Copyright (c) Feross Aboukhadijeh.
FAQs
Simplest way to make http get requests. Supports HTTPS, redirects, gzip/deflate, streams in < 100 lines.
The npm package simple-get receives a total of 4,310,112 weekly downloads. As such, simple-get popularity was classified as popular.
We found that simple-get demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.